package com.ruoyi.carton.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.carton.domain.Cardboard;
import com.ruoyi.carton.domain.OrderRequest;
import com.ruoyi.carton.domain.OrderStatus;
import com.ruoyi.carton.domain.Orders;
import com.ruoyi.carton.domain.bo.OrderRequestWithCardboardBO;
import com.ruoyi.carton.domain.vo.OrderRequestQuery;
import com.ruoyi.carton.domain.vo.OrderRequestVO;
import com.ruoyi.carton.mapper.OrderRequestMapper;
import com.ruoyi.carton.service.CardboardService;
import com.ruoyi.carton.service.OrderRequestService;
import com.ruoyi.carton.service.OrdersService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
* @author 时空
* @description 针对表【order_request(请购纸板表)】的数据库操作Service实现
* @createDate 2025-06-28 13:26:40
*/
@Service
public class OrderRequestServiceImpl extends ServiceImpl<OrderRequestMapper, OrderRequest>
    implements OrderRequestService{
    @Autowired
    private CardboardService cardboardService;

    @Autowired
    private OrderRequestMapper orderRequestMapper;
    @Autowired
    private OrdersService ordersService;


    @Transactional
    @Override
    public void addOrUpdateCardboardAndBindToRequest(Integer  requestId, OrderRequestVO vo) {

        //先通过requestId查询cardboardId是否存在
        OrderRequest existRequest = this.getById(requestId);
        if (existRequest == null) {
            throw new ServiceException("请购单不存在");
        }
        String cardboardId;
        if (existRequest.getCardboardId() != null) {
            // --- 情况1：更新已存在的 cardboard ---
            cardboardId = existRequest.getCardboardId();
            Cardboard updateCardboard = new Cardboard();
            updateCardboard.setId(cardboardId);
            updateCardboard.setFluteType(vo.getFluteType());
            updateCardboard.setLayerCount(vo.getLayerCount());
            updateCardboard.setMaterialCode(vo.getMaterialCode());
            updateCardboard.setSizeLength(vo.getSizeLength());
            updateCardboard.setSizeWidth(vo.getSizeWidth());
            updateCardboard.setScoringLine(vo.getScoringLine());
            updateCardboard.setMaterialDesc(vo.getMaterialDesc());
            updateCardboard.setCrushStrength(vo.getCrushStrength());
            updateCardboard.setEdgeCrush(vo.getEdgeCrush());
            updateCardboard.setBurstStrength(vo.getBurstStrength());
            updateCardboard.setUnitPrice(vo.getUnitPrice());

            cardboardService.updateById(updateCardboard);
        }
        else {
            // --- 情况2：创建新的 cardboard ---
            cardboardId = UUID.randomUUID().toString().replace("-", "");
            Cardboard newCardboard = new Cardboard();
            newCardboard.setId(cardboardId);
            newCardboard.setFluteType(vo.getFluteType());
            newCardboard.setLayerCount(vo.getLayerCount());
            newCardboard.setMaterialCode(vo.getMaterialCode());
            newCardboard.setSizeLength(vo.getSizeLength());
            newCardboard.setSizeWidth(vo.getSizeWidth());
            newCardboard.setScoringLine(vo.getScoringLine());
            newCardboard.setMaterialDesc(vo.getMaterialDesc());
            newCardboard.setCrushStrength(vo.getCrushStrength());
            newCardboard.setEdgeCrush(vo.getEdgeCrush());
            newCardboard.setBurstStrength(vo.getBurstStrength());
            newCardboard.setUnitPrice(vo.getUnitPrice());

            cardboardService.save(newCardboard);

        }
        // --- 更新 cardboard_request 表绑定信息 ---
        OrderRequest request = new OrderRequest();
        request.setId(requestId);
        request.setCardboardId(cardboardId);
        request.setOrderQuantity(vo.getOrderQuantity());
        request.setArea(vo.getArea());
        request.setFactoryAddress(vo.getFactoryAddress());
        request.setSalesman(vo.getSalesman());
        request.setSalesManager(vo.getSalesManager());
        request.setApprover(vo.getApprover());

        this.updateById(request);
    }

    @Override
    public OrderRequestWithCardboardBO getWithCardboardById(Integer id) {
        return orderRequestMapper.selectWithCardboardById(id);
    }

    @Override
    public List<OrderRequestWithCardboardBO> listWithBoxAndCardboard(OrderRequestQuery query) {
        return orderRequestMapper.selectWithBoxAndCardboardList(query);
    }

    @Override
    public void createOrderRequest(Orders order) {
        if (order == null || order.getId() == null) {
            throw new IllegalArgumentException("订单信息不能为空");
        }

        OrderRequest request = new OrderRequest();
        request.setOrderId(order.getId());             // 设置订单编号
        request.setBoxProductId(order.getProductId()); // 设置产品编号（来自box表）

        this.save(request); // 保存到数据库

    }

    @Override
    public void removeOrderRequestWithCardboard(Integer id) {
        // 查询请购单
        OrderRequest orderRequest = this.getById(id);
        if (orderRequest == null) {
            throw new ServiceException("请购单不存在");
        }
        // 查询订单
        Orders order = ordersService.getById(orderRequest.getOrderId());
        if (order == null) {
            throw new ServiceException("关联的订单不存在");
        }
        // 检查订单状态是否是“请购”
        if (order.getStatus() != OrderStatus.REQUESTED) {
            throw new ServiceException("当前订单状态不是请购状态，不能删除请购单");
        }
        // 删除请购单
        this.removeById(id);

        // 删除纸板信息
        if (StringUtils.isNotEmpty(orderRequest.getCardboardId())) {
            cardboardService.removeById(orderRequest.getCardboardId());
        }
        // 更新订单状态为草稿
        order.setStatus(OrderStatus.DRAFT);
        ordersService.updateById(order);
    }

//    @Override
//    public List<OrderRequestWithCardboardBO> listWithBoxAndCardboard() {
//        return orderRequestMapper.selectWithBoxAndCardboardList();
//    }

}




